//
//  DLValidation.m
//  DGGCommonSDK
//
//  Created by dgg_hzk on 2019/11/20.
//

#import "DLValidation.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>

@implementation DLValidation

+ (BOOL)isMoney:(NSString *)str{
    if (!str) {
        return NO;
    }else if (str.length >0) {
        //\\+|
        NSString *emailRegex = @"(\\-)?(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,8}(([.]\\d{0,2})?)))?";//(带正负号的)
        //NSString *emailRegex = @"^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:str];
    }
    return NO;
}

+ (BOOL)isCardIDWithStr:(NSString *)str{
    if (!str) {
        return NO;
    }else if (str.length  == 15 ||str.length  == 18 ) {
        NSString *emailRegex = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X|x)$)";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:str];
    }else{
        return NO;
    }
}

+ (BOOL)isfullNameWithStr:(NSString *)str{
    if (!str) {
        return NO;
    }else {
        NSString *regex = @"^[\s\a-zA-Z\u4e00-\u9fa5\\.\\*\\)\\(\\+\\$\\[\\?\\\\\\^\\{\\|\\]\\}%%%@\'\",。‘、-【】·！_——=:;；<>《》‘’“”!#~]{2,20}";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [emailTest evaluateWithObject:str];
    }
}

+ (BOOL)validateMobile:(NSString *)mobile{
    NSString *mobileRegex = @"[1][3,4,5,6,7,8,9][0-9]{9}";
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex];
    return [mobileTest evaluateWithObject:mobile];
}

+ (BOOL)isLandlineWithNum:(NSString  *)num{
    NSString *mobileRegex = @"(\\d{3,4}\\-)?\\d{7,8}";
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex];
    NSString *mobileRegex2 = @"0(([1-9]\\d)|([3-9]\\d{2}))\\d{8}";
    NSPredicate *mobileTest2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobileRegex2];
    return [mobileTest evaluateWithObject:num]||[mobileTest2 evaluateWithObject:num];
}

+ (BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

+ (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

+ (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}

+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

+(BOOL)validateCamera
{
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
        
    {
        return NO;
    }
    return YES;
}

+(BOOL)validateCameraPhotoAlbum
{
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    
    if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied)
    {
        return NO;
    }
    return YES;
    
}
@end
